home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS in a Box 7
/
BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso
/
Files
/
Prog
/
M
/
MacStarter.cpt
/
inputBoxes.c
< prev
next >
Wrap
Text File
|
1992-10-11
|
9KB
|
326 lines
/* This file defines some functions for using Mac dialog boxes.
See the README file that should be in the same folder with this file.
*/
#include "inputBoxes.h" // also includes stdio.h
#include "ctype.h"
#include "stdlib.h"
#include "errno.h"
void TellUser(const Str255 message) {
ParamText(message,"","","");
NoteAlert(129,0L);
}
short AskUser(const Str255 question, short* answeredYes) {
short temp;
ParamText(question,"","","");
temp = Alert(130,0L);
*answeredYes = (temp == ok);
return (temp != cancel);
}
void AskUserWithoutCancel(const Str255 question, short* answeredYes) {
short temp;
ParamText(question,"","","");
temp = Alert(131,0L);
*answeredYes = (temp == ok);
}
DialogRecord dlgRec;
DialogPtr dlg;
short maxStringLength;
long maxLong, minLong;
double maxDouble, minDouble;
long inputLong;
double inputDouble;
char inputStr[257];
const unsigned char *thePrompt;
Handle inputItem;
ControlHandle okBttn, cancelBttn;
enum {
getStr,
getLong,
getDouble,
getWord
} dlgType;
short RunDialog(void);
pascal Boolean dlgFilter(DialogPtr dlg, EventRecord *theEvent, short *item);
short CheckInput(void);
short InputString(const Str255 prompt, Str255 str, const short maxLength) {
short temp;
short i;
maxStringLength = ( maxLength > 0 && maxLength < 256) ? maxLength : 255;
if (maxStringLength <= 32)
dlg = GetNewDialog(132,&dlgRec,(Ptr) -1);
else
dlg = GetNewDialog(133,&dlgRec,(Ptr) -1);
dlgType = getStr;
thePrompt = prompt;
temp = RunDialog();
if (temp)
for (i=0;i<=inputStr[0];i++)
str[i] = inputStr[i];
return temp;
}
short InputWord(const Str255 prompt, Str255 str, const short maxLength) {
short temp;
short i;
maxStringLength = ( maxLength > 0 && maxLength < 256) ? maxLength : 255;
if (maxStringLength <= 32)
dlg = GetNewDialog(132,&dlgRec,(Ptr) -1);
else
dlg = GetNewDialog(133,&dlgRec,(Ptr) -1);
dlgType = getWord;
thePrompt = prompt;
temp = RunDialog();
if (temp)
for (i=0;i<=inputStr[0];i++)
str[i] = inputStr[i];
return temp;
}
short InputLongInt(const Str255 prompt, long* n, const long min, const long max) {
short temp;
maxLong = max;
minLong = min;
dlg = GetNewDialog(132,&dlgRec,(Ptr) -1);
dlgType = getLong;
thePrompt = prompt;
temp = RunDialog();
if (temp)
*n = inputLong;
return temp;
}
short InputDouble(const Str255 prompt, double* x, const double min, const double max) {
short temp;
maxDouble = max;
minDouble = min;
dlg = GetNewDialog(132,&dlgRec,(Ptr) -1);
dlgType = getDouble;
thePrompt = prompt;
temp = RunDialog();
if (temp)
*x = inputDouble;
return temp;
}
short RunDialog() {
short hit;
short itemType;
Rect box;
GrafPtr savePort;
GetPort(&savePort);
ParamText(thePrompt,"","","");
SelectWindow(dlg);
SetPort(dlg);
GetDItem(dlg, 2, &itemType, &cancelBttn, &box);
GetDItem(dlg, 1, &itemType, &okBttn, &box);
InsetRect(&box,-4,-4);
PenSize(3,3);
FrameRoundRect(&box,16,16);
GetDItem(dlg, 4, &itemType, &inputItem, &box);
for (;;) {
ModalDialog(&dlgFilter,&hit);
if (hit == ok) {
if (CheckInput())
break;
}
else if (hit == cancel) {
break;
}
};
DisposeDialog(dlg);
return (hit == ok);
}
pascal Boolean dlgFilter(DialogPtr dlg, EventRecord *theEvent, short *item) {
Boolean eventOK;
long junk;
short ch;
if ( (theEvent->what == keyDown || theEvent->what == autoKey)
&& !(theEvent->modifiers & cmdKey) ) {
ch = theEvent->message & 0xFF;
if ( ch == 13 || ch == 3 ) {
HiliteControl(okBttn,1);
Delay(10,&junk);
HiliteControl(okBttn,0);
*item = ok;
return true;
}
else if ( ch == 27 ) {
HiliteControl(cancelBttn,1);
Delay(10,&junk);
HiliteControl(cancelBttn,0);
*item = cancel;
return true;
}
else if ( (ch <= 31 && ch >= 28) || (ch == 8) ) {
return false;
}
else {
switch ( dlgType ) {
case getWord:
eventOK = isalpha(ch);
break;
case getStr:
eventOK = (ch > 31);
break;
case getLong:
eventOK = isdigit(ch) || (ch == '-' && (minLong < 0 || minLong >= maxLong));
break;
case getDouble:
eventOK = isdigit(ch) || (ch == '.') || (ch == 'e')
|| (ch == 'E') || (ch == '+') || (ch == '-');
};
if ( !eventOK ) {
*item = 0;
SysBeep(1);
};
return !eventOK;
}
}
else
return false;
}
short CheckInput(void) {
short dataOK;
char* endptr;
char message[256];
GetIText(inputItem,inputStr);
switch (dlgType) {
case getStr:
dataOK = (inputStr[0] > 0) && (inputStr[0] <= maxStringLength);
if (!dataOK) {
sprintf(message,
"Please input a string of between 1 and %i characters.",
maxStringLength);
};
break;
case getWord:
dataOK = (inputStr[0] > 0) && (inputStr[0] <= maxStringLength);
if (!dataOK) {
sprintf(message,
"Please input a string of between 1 and %i letters.",
maxStringLength);
};
break;
case getLong:
if (inputStr[0] == 0)
dataOK = 0;
inputStr[inputStr[0]+1] = 0;
errno = 0;
inputLong = strtol(inputStr+1,&endptr,10);
dataOK = (errno == 0)
&& (*endptr == 0)
&& ((minLong >= maxLong)
|| (inputLong >= minLong && inputLong <= maxLong));
if (!dataOK) {
if (maxLong <= minLong)
sprintf(message,"Please input a legal integer.");
else
sprintf(message,
"Please input a legal integer in the range from %li to %li.",
minLong, maxLong);
};
break;
case getDouble:
if (inputStr[0] == 0)
dataOK = 0;
inputStr[inputStr[0]+1] = 0;
errno = 0;
inputDouble = strtod(inputStr+1,&endptr);
dataOK = (errno == 0)
&& (*endptr == 0)
&& ((minDouble >= maxDouble)
|| (inputDouble >= minDouble && inputDouble <= maxDouble));
if (!dataOK) {
if (maxDouble <= minDouble)
sprintf(message,"Please input a legal number.");
else
sprintf(message,
"Please input a legal number in the range from %g to %g.",
minDouble, maxDouble);
};
};
if (!dataOK) {
CtoPstr(message);
ParamText(message,"","","");
StopAlert(135,0L);
ParamText(thePrompt,"","","");
SelIText(dlg,4,0,32000);
};
return dataOK;
}
FILE* OpenNewFile(const Str255 prompt, Str255 fileName) {
Point where;
SFReply reply;
FILE *file;
short i;
OSErr err;
where.h = 20;
where.v = 60;
SFPutFile(where, prompt, fileName, 0, &reply);
if (reply.good) {
err = Create(reply.fName,reply.vRefNum,'ttxt','TEXT');
if (err == dupFNErr) {
err = FSDelete(reply.fName, reply.vRefNum);
if (err != noErr) {
TellUser("\pSorry, unable to delete existing file of the same name.");
return 0;
};
err = Create(reply.fName,reply.vRefNum,'ttxt','TEXT');
};
if (err == wPrErr || err == vLckdErr) {
TellUser("\pSorry, unable to write to that disk; it is locked or write protected.");
return 0;
};
if (err != noErr) {
TellUser("\pSorry, an error occured while trying to create the new file.");
return 0;
};
if (fileName)
for (i=0; i<=reply.fName[0]; i++)
fileName[i] = reply.fName[i];
SetVol(0,reply.vRefNum);
PtoCstr(reply.fName);
file = fopen((char*)reply.fName,"w");
if (!file)
TellUser("\pSorry, an error occured while trying to open the new file.");
return file;
}
else
return 0;
}
FILE* OpenOldFile(Str255 fileName) {
Point where;
SFTypeList typeList;
SFReply reply;
FILE *file;
short i;
typeList[0] = 'TEXT';
where.h = 20;
where.v = 60;
SFGetFile(where, 0, 0, 1, &typeList, 0, &reply);
if (reply.good) {
if (fileName)
for (i=0; i<=reply.fName[0]; i++)
fileName[i] = reply.fName[i];
SetVol(0,reply.vRefNum);
PtoCstr(reply.fName);
file = fopen((char*)reply.fName,"r");
if (!file)
TellUser("\pSorry, an error occured while trying to open that file for reading.");
return file;
}
else
return 0;
}